/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Jul 12, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.control.tools;

import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;
import edu.psu.geovista.app.pcp.plot.layer.AxisLayer;
import edu.psu.geovista.app.pcp.plot.layer.DataLayer;
import edu.psu.geovista.app.pcp.plot.layer.DynamicLayer;
import edu.psu.geovista.app.plot.axis.AxisList;
import edu.psu.geovista.app.plot.axis.YAxis;
import edu.psu.geovista.app.plot.layer.Layer;

import java.awt.*;
import java.awt.event.MouseEvent;

public class AxisMoveTool extends AxisOrderTool{
    AxisList activeAxisList; //support move multiple axis concurrently

    public AxisMoveTool(BasicPcpUI ui) {
        super(ui);

    }
     public void mouseDragged(MouseEvent e) {
         Point cp=e.getPoint() ;
         if (this.activeAxis !=null&&
             this.isAxisMovable(this.activeAxis,cp.x-this.lastp.x ) )  {
             //if the axis is NOT movable, won't draw preview Axis
             this.updateLCpoint(e.getPoint()) ; //keep the last point as preview Axis's location

              //draw preview axis moving

                DynamicLayer dl=(DynamicLayer)ui.getLayer(Layer.DYNAMIC_LAYER );
                dl.previewMoveAxis(activeAxis,0,cp.x);
                this.repaintPlot() ;
         }


     }

      public void mouseReleased(MouseEvent e) {

          if (activeAxis!=null){



            int deltaX=this.lastp.x-activeAxis.getLocation().x;
              if (this.isAxisMovable(activeAxis,deltaX) ){
                this.moveAxis(activeAxis,deltaX);
              }
          }

           super.mouseReleased(e);
      }
     /**
      * Move axis horizontally
      * @param axis
      * @param deltaX   change of x in term of graphic value
      */
     public void moveAxis(YAxis axis, int deltaX) {
         this.setActiveAxis(axis);
         //1. update axis location
         int y=this.activeAxis.getLocation().y;
         int x=this.activeAxis.getLocation().x+deltaX;
         //this.activeAxis.setLocation(x, y);
         this.activeAxis.setAxisLocation(x, y);

         //update interval
         this.xAxis=getUI().getPlot().getXaxis() ;
         int index=activeAxis.getIndex();
         int newInteval=xAxis.getInterval(index)+deltaX;
         this.xAxis.setIntervalByIndex(index,newInteval) ;
         //upate interval for axis right to activeAxis
         if (activeAxis.getIndex() !=this.getNumOfAxis() -1) {//won't update if the axis is the last index
         newInteval=xAxis.getInterval(index+1)-deltaX;
         this.xAxis.setIntervalByIndex(index+1,newInteval) ;
         }


         //update view
          AxisLayer axisLayer=(AxisLayer)this.ui.getLayer(Layer.AXIS_LAYER );
          axisLayer.clearLayer() ;
          axisLayer.paintLayer() ;

          Object[] dataLys=this.getLayerByType(Layer.TYPE_DATA );
         for (int i=0;i<dataLys.length ;i++){
             DataLayer dataLayer=(DataLayer)dataLys[i];//(DataLayer)this.ui.getLayer(Layer.AllData_LAYER );
             int[] axs=new int[]{this.activeAxis.getIndex()};

             dataLayer.repaintAxis(axs);
         }
         this.repaintPlot() ;
         //repaintDataLayers();

     }
     /**
      *
      * @param axis
      * @param deltaX  change of x
      * @return   true if axis is allowed to move with deltaX
      */
     protected boolean isAxisMovable(YAxis axis,int deltaX) {
         int minInterval = this.getUI().getPlot().getXaxis().getMinInterval();

         if(! axis.isMovable() )return false;
         if (axis.getIndex()==this.getNumOfAxis() -1||
             axis.getIndex()==0) {//not move axis at both end
             return false;
         }

         //int deltaX=cp.x-lastp.x ;
         int targetLocX=axis.getLocation().x+deltaX;//target location's x
         int distance=0;  //distance between target location and closest axis. The distance should be more than MIN_INTERVAL
         if (deltaX>0){//move to right

             YAxis toAxis=this.getAxisList().getAxis(axis.getIndex() +1);//the axis right to the activeAxis
             distance=toAxis.getLocation().x-targetLocX;
             if(distance>minInterval/*edu.psu.geovista.app.pcp.axis.XAxisOrder.MIN_INTERVAL*/ ){
                 return true;
             }
             else
                 return false;
         }
         else if(deltaX<0){//move to left

             YAxis toAxis=this.getAxisList().getAxis(axis.getIndex() -1);//the axis is left to the activeAxis
             distance=targetLocX-toAxis.getLocation().x;
             if(distance>minInterval/*XAxisOrder.MIN_INTERVAL*/ ){
                 return true;
             }
             else
                 return false;
         }
         else{//
             return true;
         }


     }


}
